﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfApplication1
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            DataContext = new MainWindowViewModel();
        }

        private void OnCurrentCellChanged(object sender, EventArgs e)
        {
            //object endOfList = null;

            //if (dgLines.CurrentCell.Column != null)
            //{
            //    if (dgLines.CurrentCell.Column.DisplayIndex == 2)
            //    {
            //        if (dgLines.CurrentCell.Item == endOfList)
            //        {
            //            dgLines.MoveFocus(new TraversalRequest(FocusNavigationDirection.Down));
            //        }
            //    }
            //    else if (dgLines.CurrentCell.Column.DisplayIndex == dgLines.Columns.Count - 1)
            //    {
            //        PressKey(Key.Return);
            //        DataGridRow row = dgLines.GetSelectedRow();
            //        row.MoveFocus(new TraversalRequest(FocusNavigationDirection.First));
            //    }
            //}
        }

        private void OnKeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                // when no select row, we get the row the have selected cell.
                DataGridRow selectedRow = dgLines.GetSelectedRow() ?? dgLines.GetCurrentRow();

                // select the row if it is not selected
                if (selectedRow != null && !selectedRow.IsSelected)
                    selectedRow.Focus();

                // if we couldn't get or select row, exit.
                if (selectedRow == null)
                    return;

                // get current cell
                int colIndex = dgLines.Columns.IndexOf(dgLines.CurrentColumn);
                DataGridCell currentCell = dgLines.GetCell(selectedRow, colIndex);

                if (currentCell.IsEditing)
                {
                    dgLines.CommitEdit();

                    // if reached to the last column
                    if (dgLines.CurrentCell.Column.DisplayIndex == dgLines.Columns.Count - 1)
                    {
                        PressKey(Key.Return);

                        // get the new row and move focuse to the first cell.
                        DataGridRow newRow = dgLines.GetSelectedRow();
                        newRow.MoveFocus(new TraversalRequest(FocusNavigationDirection.First));
                    }
                    else
                    {
                        currentCell.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next) { Wrapped = true });
                    }
                }

                dgLines.BeginEdit();

                dgLines.SelectedItem = dgLines.CurrentItem;
                e.Handled = true;
                dgLines.UpdateLayout();
            }
        }

        private void PressKey(Key key)
        {
            KeyEventArgs args = new KeyEventArgs(Keyboard.PrimaryDevice, Keyboard.PrimaryDevice.ActiveSource, 0, key);
            args.RoutedEvent = Keyboard.KeyDownEvent;
            InputManager.Current.ProcessInput(args);
        }

        private void UserControl_MouseEnter(object sender, MouseEventArgs e)
        {

        }
    }
}
